﻿using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.IO;
using ShootSearch.Util;

namespace ShootSearch.Logging
{
    public class Log
    {
        /// <summary>
        /// Default log configuration file
        /// </summary>
        public const string LOG_CONFIG = "log4net.config";
        public static ILog Logger = null;

        #region Initialize
        static Log()
        {
            Watch();
        }

        public static void Watch()
        {
            Watch(IO.StartupPath + "\\" + LOG_CONFIG);
        }

        public static void Watch(string path)
        {
            FileInfo fi = new FileInfo(path);
            if (fi.Exists)
            {
                log4net.Config.XmlConfigurator.ConfigureAndWatch(fi);
                Logger = log4net.LogManager.GetLogger("Default");
            }
        } 
        #endregion

        #region Log Methods

        #region Info
        public static void Info(ILog logger, object msg, params object[] args)
        {
            if (logger != null && msg != null)
            {
                if (args.Length == 0)
                {
                    logger.Info(msg);
                }
                else
                {
                    logger.InfoFormat(msg.ToString(), args);
                }
            }
        }

        public static void Info(ILog logger, object msg, Exception ex)
        {
            if (logger != null)
            {
                logger.Info(msg, ex);
            }
        } 
        #endregion

        #region Warn
        public static void Warn(ILog logger, object msg, Exception ex)
        {
            if (logger != null)
            {
                logger.Warn(msg, ex);
            }
        }

        public static void Warn(ILog logger, object msg, params object[] args)
        {
            if (logger != null && msg != null)
            {
                if (args.Length == 0)
                {
                    logger.Warn(msg);
                }
                else
                {
                    logger.WarnFormat(msg.ToString(), args);
                }
            }
        } 
        #endregion

        #region Error
        public static void Error(ILog logger, object msg, Exception ex)
        {
            if (logger != null)
            {
                logger.Error(msg, ex);
            }
        }

        public static void Error(ILog logger, object msg, params object[] args)
        {
            if (logger != null && msg != null)
            {
                if (args.Length == 0)
                {
                    logger.Error(msg);
                }
                else
                {
                    logger.ErrorFormat(msg.ToString(), args);
                }
            }
        } 
        #endregion

        #region Debug
        public static void Debug(ILog logger, object msg, Exception ex)
        {
            if (logger != null)
            {
                logger.Debug(msg, ex);
            }
        }

        public static void Debug(ILog logger, object msg, params object[] args)
        {
            if (logger != null && msg != null)
            {
                if (args.Length == 0)
                {
                    logger.Debug(msg);
                }
                else
                {
                    logger.DebugFormat(msg.ToString(), args);
                }
            }
        } 
        #endregion

        #region Fatal
        public static void Fatal(ILog logger, object msg, Exception ex)
        {
            if (logger != null)
            {
                logger.Fatal(msg, ex);
            }
        }

        public static void Fatal(ILog logger, object msg, params object[] args)
        {
            if (logger != null && msg != null)
            {
                if (args.Length == 0)
                {
                    logger.Fatal(msg);
                }
                else
                {
                    logger.FatalFormat(msg.ToString(), args);
                }
            }
        }
        #endregion
        #endregion
    }
}
